/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.skipass;

import java.util.Calendar;
import ua.yandex.MyList.MyLinkedList;
import ua.yandex.MyList.MyList;
import ua.yandex.skipass.SkiPass.PassType;

/**
 *
 * @author Brothers
 */
public class AccountingSystem {

    private MyList list;
    private int idCounter = 0;
    //private int  date;
    private Calendar date;
    private long allAccessTrue = 0;
    private long allAccessFalse = 0;
    private Turnstile turn;

    public AccountingSystem() {
        this.list = new MyLinkedList();
        this.idCounter = 0;
        //this.date = 0;
        this.date.getInstance();
        this.turn = new Turnstile();
    }

    public void plusAccessFalse() {
        this.allAccessFalse++;
    }

    public void plusAccessTrue() {
        this.allAccessTrue++;
    }

    public Calendar getDate() {
        //return this.date;
        return this.date;
    }

    public void setDate(Calendar data) {
        this.date.setTime(data.getTime());
    }

    public void addPass(SkiPass pass) {
        this.list.add(pass);
    }

    public SkiPass createSkipass(PassType type, int k) {
        this.idCounter++;
        SkiPass pass;
        Week ogr;
        if (type.equals(PassType.Work)) {
            ogr = Week.values()[k];
            if (k / 2 == 0) {
                pass = new Day();

            } else {
                pass = new Lift();
            }
        } else {
            pass = new Season();
            ogr = Week.None;
        }
        pass.setSkypass(this.idCounter, type, this.date, ogr);
        return pass;
    }

    public boolean wrongDay(SkiPass pass) {
        return pass.isDayInOgr(this.date);
    }

    public boolean checkPass(SkiPass pass) {
        if (!pass.getPassAccess() && this.wrongDay(pass)) {
            this.plusAccessFalse();
            return false;
        }
        return isPassRight(pass);
    }

    public boolean isPassRight(SkiPass pass) {
        if (pass instanceof Season) {
            if (!pass.isValidDate(this.getDate())) {
                pass.blockPass();
                this.plusAccessFalse();
                return false;
            }
        } else if (pass instanceof Lift && pass.getNumberLift() == 0) {
            pass.blockPass();
            this.plusAccessFalse();
            return false;
        }
        if (pass instanceof Lift) {
            pass.setNumberLift(pass.getNumberLift() - 1);
        }
        this.plusAccessTrue();
        return true;
    }

    public void createTurnstile() {
        this.turn = new Turnstile(this);
    }
}
